<?php
/**
 * 功能: 商品管理
 * User: WD
 * Date: 2018/10/11 0011
 * Time: 10:26
 */

namespace app\admin\model;
use think\Db;
use think\Model;

class Goods extends Model{
    /**
     * 价格范围搜索
     * @param $start_price
     * @param $end_price
     */
    public function searchPrice($start_price,$end_price){
        if(!empty($start_price)&& !empty($end_price)){
            $goods = Db::table('goods')
                ->where('goods_price','>=',$start_price)
                ->where('goods_price','<=',$end_price)
                ->whereTime('goods_end_time','>=',time())
                ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                    $gt_id = $item['gt_id'];
                    $gb_id = $item['gb_id'];
                    if(!empty($gt_id)){
                        $item['gt_name'] = Db::name('goods_type')->where('id',$gt_id)->value('name');
                    }
                    if(!empty($gb_id)){
                        $item['gb_name'] = Db::name('icon')->where('id',$gb_id)->value('name');
                    }
                    return $item;

                });
        }elseif(!empty($start_price)&& empty($end_price)){
            $goods = Db::table('goods')
                ->where('goods_price','>=',$start_price)
                ->whereTime('goods_end_time','>=',time())
                ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                    $gt_id = $item['gt_id'];
                    $gb_id = $item['gb_id'];
                    if(!empty($gt_id)){
                        $item['gt_name'] = Db::name('goods_type')->where('id',$gt_id)->value('name');
                    }
                    if(!empty($gb_id)){
                        $item['gb_name'] = Db::name('icon')->where('id',$gb_id)->value('name');
                    }
                    return $item;

                });
        }elseif(empty($start_price)&& !empty($end_price)){
            $goods = Db::table('goods')
                ->where('goods_price','<=',$end_price)
                ->whereTime('goods_end_time','>=',time())
                ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                    $gt_id = $item['gt_id'];
                    $gb_id = $item['gb_id'];
                    if(!empty($gt_id)){
                        $item['gt_name'] = Db::name('goods_type')->where('id',$gt_id)->value('name');
                    }
                    if(!empty($gb_id)){
                        $item['gb_name'] = Db::name('icon')->where('id',$gb_id)->value('name');
                    }
                    return $item;

                });
        }
        return $goods;

    }
    public function countPrice($start_price,$end_price){
        if(!empty($start_price)&& !empty($end_price)){
            $count = Db::table('goods')
                ->where('goods_price','>=',$start_price)
                ->where('goods_price','<=',$end_price)
                ->whereTime('goods_end_time','>=',time())
                ->count();
        }elseif(!empty($start_price)&& empty($end_price)){
            $count = Db::table('goods')
                ->where('goods_price','>=',$start_price)
                ->whereTime('goods_end_time','>=',time())
                ->count();
        }elseif(empty($start_price)&& !empty($end_price)){
            $count = Db::table('goods')
                ->where('goods_price','<=',$end_price)
                ->whereTime('goods_end_time','>=',time())
                ->count();
        }
        return $count;
    }

}